﻿using System;
using System.Timers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ScreensInterfaces
{
    public class ScreenBase : UserControl, IScreen
    {
        protected ScreenInput context = new ScreenInput();
        private Timer timeOutTimer = new Timer();

        public event EventHandler<UserInputReadyEventArgs> UserEnteredInput;

        public event EventHandler ScreenTimedOut;

        public ScreenBase()
        {
            this.DataContext = context;
            timeOutTimer.Elapsed += OnTimedEvent;
        }

        virtual public void UpdateScreen(ScreenInput input)
        {
            //since wf thread cannot change any thing on the ui
            //here we change the object that ui is binding to, 
            //so that ui thread will then update the ui
            //
            //another way would be to marshal the change to the ui thread
            //check ShowMessageScreen.cs
            //
            //also check http://www.albahari.com/threading/part2.aspx

            context.Header = input.Header;
            context.Footer = input.Footer;
            context.Text1 = input.Text1;
            context.Text2 = input.Text2;
            context.Text3 = input.Text3;
            context.Text4 = input.Text4;
            context.Note = input.Note;
            context.Images = input.Images;
            context.Choices = input.Choices;
            context.IsShowBackButton = input.IsShowBackButton;
            context.IsShowCancelButton = input.IsShowCancelButton;
            context.IsShowMainMenuButton = input.IsShowMainMenuButton;
            context.IsShowNextButton = input.IsShowNextButton;
            context.IsShowOkayButton = input.IsShowOkayButton;

            if (input.TimeOut > 0)
            {
                timeOutTimer.Interval = input.TimeOut;
                timeOutTimer.Enabled = true;
            }
        }

        protected void RaiseUserInputReadyEvent(UserInputReadyEventArgs e)
        {
            EventHandler<UserInputReadyEventArgs> handler = UserEnteredInput;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void RaiseUserClickedBackEvent()
        {
            UserInputReadyEventArgs e = new UserInputReadyEventArgs();
            EventHandler<UserInputReadyEventArgs> handler = UserEnteredInput;
            e.ScreenOutput.isBackButtonClicked = true;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void RaiseOnScreenTimedOut(EventArgs e)
        {
            EventHandler handler = ScreenTimedOut;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            RaiseOnScreenTimedOut(new EventArgs());
            timeOutTimer.Enabled = false;
        }
    }
}
